﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TPCN.DAL;
using System.Globalization;
using TPCN.Common;

namespace TPCN
{
    public partial class Chitietsanpham : System.Web.UI.Page
    {
        private static string HangID;
        private static HangObj obj;
        private const string NoImageUrl = "~/App_Themes/Default/images/no_image.gif";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                LoadData();
        }

        protected bool GetProductIdFromUrl()
        {
            if (Request.QueryString["id"] != null && Request.QueryString["id"].ToString().Trim() != "")
            {
                HangID = Request.QueryString["id"].ToString();
                return true;
            }
            return false;
        }

        protected void LoadData()
        {
            if (GetProductIdFromUrl() && (obj = HangDAL.GetByID(HangID)).Tenhang != null)
            {
                lblTitle.Text = obj.Tenhang;
                lblTenhang.Text = obj.Tenhang;
                lblGiabanle.Text = obj.Giabanle;
                lblGiathitruong.Text = obj.Giathitruong;
                lblThongtinsoluoc.Text = obj.Thongtinsoluoc;
                lblThongtinchitiet.Text = obj.Thongtinchitiet;
                lblGiam.Text = ProcessDiscount(obj.Giathitruong, obj.Giabanle);
                imgProduct.ImageUrl = SetImageUrl(obj.Anh);
                txtSoluong.Text = "1";

            }
            else
                Response.Redirect("~/Default.aspx");
        }

        protected string ProcessDiscount(string _giathitruong, string _giabanle)
        {
            double giathitruong = Convert.ToDouble(_giathitruong);
            double giabanle = Convert.ToDouble(_giabanle);
            if (giathitruong > 0)
            {
                double discount = 100 - ((giabanle / giathitruong) * 100);
                return discount.ToString("N0", CultureInfo.CurrentCulture) + "%";
            }
            return "0";
        }

        protected string SetImageUrl(string url)
        {
            return (String.IsNullOrEmpty(url)) ? NoImageUrl : url;
        }

        protected void btnAddtocart_Click(object sender, ImageClickEventArgs e)
        {
            ShoppingCartObj cart;
            ShoppingCartItem item;
            if (HttpContext.Current.Session["Cart"] == null)
            {
                cart = new ShoppingCartObj();
                HttpContext.Current.Session["cart"] = cart;
            }
            else
            {
                cart = (ShoppingCartObj)HttpContext.Current.Session["cart"];
            }
            item = new ShoppingCartItem();
            item.HangID = HangID;
            item.Tenhang = lblTenhang.Text;
            item.Soluong = Convert.ToInt32(txtSoluong.Text.Trim());
            item.Dongia = Convert.ToDouble(lblGiabanle.Text);
            cart.Insert(item);
            Response.Redirect("~/Giohang.aspx");
        }
    }
}